﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FetionWebMessenger.Util;
namespace FetionWebMessenger.AddWebService
{
    public class SubscribeManager
    {
        static public System.Timers.Timer myTimer = new System.Timers.Timer(1000 * 600);
        static Boolean weather_not = true;
        static FetionCore fetionCore = new FetionCore();
        static List<WeatherCon> Weather_list = new List<WeatherCon>();
        public SubscribeManager(){

        }
        private void sysTimerInit()
        {
            ////10分钟执行一次                                                                                                                                           
            myTimer.Interval = 1000 * 600;
            myTimer.Elapsed += new System.Timers.ElapsedEventHandler(sysTimer_Elapsed);
            myTimer.AutoReset = true;
            myTimer.Enabled = true;
            //开始启动   
            myTimer.Start();
        }
        private void sysTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //执行事件
            DateTime time = new DateTime();
            if (time.Hour == 2)//晚上到了,计数清除
            {
                weather_not = true;
            }
            if (weather_not)
            {//如果当天的天气提示尚未执行.     
                if (time.Hour >= 18)
                {
                    //调用发送天气的方法
                    SendWeatherHint();
                    weather_not = false;
                }
            }
        }
        public void Application_Start(Object sender, EventArgs e)
        {
            //Weather_list = WeatherXML.ReadXML();
            fetionCore.LoginFetion();
            //启动定时器   
            this.sysTimerInit();
        }
        protected void SendWeatherHint()
        {
            String weather="";
            foreach (WeatherCon con in Weather_list)
            {
                weather=FetionWebMessenger.AddWebService.WeatherProvider.GetWeather(con.city);
                fetionCore.SendSMSTo(con.uri, weather);
            }
        }
        static public void AddWeatherSubscriber(String uri, String city)
        {
            WeatherCon con = new WeatherCon();
            con.uri = uri;
            con.city = city;
            Weather_list.Add(con);
            WeatherXML.WriteXML(Weather_list);
            String weather = FetionWebMessenger.AddWebService.WeatherProvider.GetWeather(con.city);
            fetionCore.SendSMSTo(con.uri, weather);
        }
    }
}
